package com.easy.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


public class ImageUtil {
    private static final String ENDPOINT="oss-cn-beijing.aliyuncs.com";
    private static final String BUCKET_NAME="tickettraveholiday";
    private static final String ACCESS_KEY_ID="LTAI5tF1yhyGYvDcHM9j8jNH";
    private static final String ACCESS_KEY_SECRET="GEQC54DNAMasJi23nrVCny3ono73BQ";

    private static final OSS ossClient = new OSSClientBuilder().build("http://"+ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);

    public static String upload(InputStream is, String fileName) {
        String newFileName= UUID.randomUUID().toString()+fileName.substring(fileName.lastIndexOf("."));
        //请求对象
        PutObjectRequest request = new PutObjectRequest(BUCKET_NAME, newFileName, is);
        //响应对象
        PutObjectResult result = ossClient.putObject(request);
        return "https://"+BUCKET_NAME+"."+ENDPOINT+"/"+newFileName;
    }



    public static String upload(MultipartFile file) {
        try {
            String originalFileName = file.getOriginalFilename();
            String newFileName = UUID.randomUUID().toString() + originalFileName.substring(originalFileName.lastIndexOf("."));
            // 请求对象
            PutObjectRequest request = new PutObjectRequest(BUCKET_NAME, newFileName, file.getInputStream());
            // 响应对象
            PutObjectResult result = ossClient.putObject(request);
            return "https://" + BUCKET_NAME + "." + ENDPOINT + "/" + newFileName;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("文件上传失败: " + e.getMessage(), e);
        }
    }

    //获取服务器所有图片
    public static List<String> getFileList() {
        List<String> list=new ArrayList<>();
        ListObjectsRequest request = new ListObjectsRequest(BUCKET_NAME);
        request.setMarker(null);
        request.setMaxKeys(1000);
        ObjectListing objectListing = ossClient.listObjects(BUCKET_NAME);
        for(OSSObjectSummary objectSummary:objectListing.getObjectSummaries()){
            list.add(objectSummary.getKey());
        }
        return list;
    }
    //删除服务器文件
    public static void deleteFile(String fileName) {
        ossClient.deleteObject(BUCKET_NAME, fileName);
    }
    //批量删除服务器文件
    public static void deleteList(List<String> fileNameList) {
        DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(BUCKET_NAME);
        deleteObjectsRequest.setKeys(fileNameList);
        ossClient.deleteObjects(deleteObjectsRequest);
    }





//    public static void main(String[] args) {
//        System.out.println(getFileList());//原来文件
//        deleteFile("0c249d7f-efc1-40d8-a38d-cd82d24671f7.jpg");
//        System.out.println(getFileList());//删除之后文件
//    }



//    public static void main(String[] args){
//        try {
////            MultipartFile mfile=null;
////            mfile.getInputStream();
//            File file=new File("D:\\1.jpg");
//            InputStream is=new FileInputStream(file);
//            String newFileName = upload(is, file.getName());
//            System.out.println(newFileName);
//        } catch (FileNotFoundException e) {
//            throw new RuntimeException(e);
//        }
//    }
//



}
